package com.loc;

import android.content.Context;
import android.text.TextUtils;
import com.amap.api.location.AMapLocation;
import java.util.List;
import org.json.JSONObject;

/* compiled from: LastLocationManager.java */
/* loaded from: classes2.dex */
public final class j {
    static fk b;
    static az e;
    static long g;
    String a = null;
    fk c = null;
    fk d = null;
    long f = 0;
    boolean h = false;
    private Context i;

    public j(Context context) {
        this.i = context.getApplicationContext();
    }

    private void e() {
        if (b == null || gb.b() - g > 180000) {
            fk f = f();
            g = gb.b();
            if (f == null || !gb.a(f.a())) {
                return;
            }
            b = f;
        }
    }

    private fk f() {
        Throwable th;
        fk fkVar;
        az azVar;
        byte[] b2;
        byte[] b3;
        String str = null;
        if (this.i == null) {
            return null;
        }
        a();
        try {
            azVar = e;
        } catch (Throwable th2) {
            th = th2;
            fkVar = null;
        }
        if (azVar == null) {
            return null;
        }
        List a = azVar.a("_id=1", fk.class);
        if (a == null || a.size() <= 0) {
            fkVar = null;
        } else {
            fkVar = (fk) a.get(0);
            try {
                byte[] b4 = p.b(fkVar.c());
                String str2 = (b4 == null || b4.length <= 0 || (b3 = fi.b(b4, this.a)) == null || b3.length <= 0) ? null : new String(b3, "UTF-8");
                byte[] b5 = p.b(fkVar.b());
                if (b5 != null && b5.length > 0 && (b2 = fi.b(b5, this.a)) != null && b2.length > 0) {
                    str = new String(b2, "UTF-8");
                }
                fkVar.a(str);
                str = str2;
            } catch (Throwable th3) {
                th = th3;
                ft.a(th, "LastLocationManager", "readLastFix");
                return fkVar;
            }
        }
        if (!TextUtils.isEmpty(str)) {
            AMapLocation aMapLocation = new AMapLocation("");
            ft.a(aMapLocation, new JSONObject(str));
            if (gb.b(aMapLocation)) {
                fkVar.a(aMapLocation);
            }
        }
        return fkVar;
    }

    public final AMapLocation a(AMapLocation aMapLocation, String str, long j) {
        if (aMapLocation == null || aMapLocation.getErrorCode() == 0 || aMapLocation.getLocationType() == 1 || aMapLocation.getErrorCode() == 7) {
            return aMapLocation;
        }
        try {
            e();
            fk fkVar = b;
            if (fkVar != null && fkVar.a() != null) {
                boolean z = false;
                if (TextUtils.isEmpty(str)) {
                    long b2 = gb.b() - b.d();
                    if (b2 >= 0 && b2 <= j) {
                        z = true;
                    }
                    aMapLocation.setTrustedLevel(3);
                } else {
                    z = gb.a(b.b(), str);
                    aMapLocation.setTrustedLevel(2);
                }
                if (z) {
                    AMapLocation a = b.a();
                    try {
                        a.setLocationType(9);
                        a.setFixLastLocation(true);
                        a.setLocationDetail(aMapLocation.getLocationDetail());
                        return a;
                    } catch (Throwable th) {
                        th = th;
                        aMapLocation = a;
                        ft.a(th, "LastLocationManager", "fixLastLocation");
                        return aMapLocation;
                    }
                }
                return aMapLocation;
            }
            return aMapLocation;
        } catch (Throwable th2) {
            th = th2;
        }
    }

    public final void a() {
        if (this.h) {
            return;
        }
        try {
            if (this.a == null) {
                this.a = fi.a("MD5", o.k());
            }
            if (e == null) {
                e = new az(this.i, az.a((Class<? extends ay>) fl.class));
            }
        } catch (Throwable th) {
            ft.a(th, "LastLocationManager", "<init>:DBOperation");
        }
        this.h = true;
    }

    public final boolean a(AMapLocation aMapLocation, String str) {
        if (this.i != null && aMapLocation != null && gb.a(aMapLocation) && aMapLocation.getLocationType() != 2 && !aMapLocation.isMock() && !aMapLocation.isFixLastLocation()) {
            fk fkVar = new fk();
            fkVar.a(aMapLocation);
            if (aMapLocation.getLocationType() == 1) {
                fkVar.a((String) null);
            } else {
                fkVar.a(str);
            }
            try {
                b = fkVar;
                g = gb.b();
                this.c = fkVar;
                fk fkVar2 = this.d;
                if (fkVar2 != null && gb.a(fkVar2.a(), fkVar.a()) <= 500.0f) {
                    return false;
                }
                if (gb.b() - this.f > 30000) {
                    return true;
                }
            } catch (Throwable th) {
                ft.a(th, "LastLocationManager", "setLastFix");
            }
        }
        return false;
    }

    public final AMapLocation b() {
        e();
        fk fkVar = b;
        if (fkVar != null && gb.a(fkVar.a())) {
            return b.a();
        }
        return null;
    }

    public final void c() {
        try {
            d();
            this.f = 0L;
            this.h = false;
            this.c = null;
            this.d = null;
        } catch (Throwable th) {
            ft.a(th, "LastLocationManager", "destroy");
        }
    }

    public final void d() {
        fk fkVar;
        String str;
        try {
            a();
            fk fkVar2 = this.c;
            if (fkVar2 != null && gb.a(fkVar2.a()) && e != null && (fkVar = this.c) != this.d && fkVar.d() == 0) {
                String str2 = this.c.a().toStr();
                String b2 = this.c.b();
                this.d = this.c;
                if (TextUtils.isEmpty(str2)) {
                    str = null;
                } else {
                    String b3 = p.b(fi.a(str2.getBytes("UTF-8"), this.a));
                    str = TextUtils.isEmpty(b2) ? null : p.b(fi.a(b2.getBytes("UTF-8"), this.a));
                    r4 = b3;
                }
                if (TextUtils.isEmpty(r4)) {
                    return;
                }
                fk fkVar3 = new fk();
                fkVar3.b(r4);
                fkVar3.a(gb.b());
                fkVar3.a(str);
                e.a(fkVar3, "_id=1");
                this.f = gb.b();
                fk fkVar4 = b;
                if (fkVar4 != null) {
                    fkVar4.a(gb.b());
                }
            }
        } catch (Throwable th) {
            ft.a(th, "LastLocationManager", "saveLastFix");
        }
    }
}
